//
//  ZSBaseTableViewController.m
//  ZhuShou
//
//  Created by Dejun Liu on 15/11/2.
//  Copyright © 2015年 jw. All rights reserved.
//

#import "ZSBaseTableViewController.h"

@interface ZSBaseTableViewController ()

@property (nonatomic,assign) BOOL isLoaded;

@end

@implementation ZSBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }

    // 设置左上角的返回按钮
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navbar_back_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
}

//- (void)goBack
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)init_addTapViewEndEdit
{
    @weakify(self);
    [self.view addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self);
        [self.view endEditing:YES];
    }];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    if (self.isLoaded == NO) {
        self.isLoaded = YES;
        [self initViews];
    }
}

- (void)initViews
{
    
}

- (void)br_pushViewControllerWithClassName:(NSString *)controllerName
{
    UIViewController *controller = [[NSClassFromString(controllerName) alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)br_dismissViewController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)br_alertViewConfirmWithTitle:(NSString *)title
                             content:(NSString *)content
                      operationIndex:(void(^)(NSInteger clickedIndex)) complation
{
    [RYCAlertView showConfirm:title message:content operation:^(NSInteger clickedIndex) {
        if (complation) {
            complation(clickedIndex);
        }
    }];
}

- (void)br_alertViewWithTitle:(NSString *)title
                      content:(NSString *)content
               operationIndex:(void(^)(NSInteger clickedIndex)) complation
{
    [RYCAlertView showHint:title message:content operation:^(NSInteger clickedIndex) {
        if (complation) {
            complation(clickedIndex);
        }
    }];
}

- (void)br_progressHUD:(BOOL) isShow
{
    if (isShow) {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }else
    {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }
    
}

- (void)br_AutoDisappearWithText:(NSString *)message
{
    NSInteger const tag = 100001;
    
    UILabel *label = [self.view viewWithTag:tag];
    UIFont *font = [UIFont systemFontOfSize:14];
    if (label) {
        [label removeFromSuperview];
    }
    
    if (1) {
        label = [[UILabel alloc] init];
        label.backgroundColor = UIColorFromRGBA(0, 0, 0, 0.7);
        label.layer.cornerRadius = 3.0f;
        label.clipsToBounds = YES;
        label.tag = tag;
        label.font = font;
        label.text = message;
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        CGSize size = [message sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil]];
        label.width = size.width+10;
        label.height = size.height+8;
        label.centerX = self.view.centerX;
        label.centerY = self.view.centerY-64;
        label.layer.opacity = 0;
        [self.view addSubview:label];
        [UIView animateWithDuration:0.5 animations:^{
            label.layer.opacity = 1;
        }];
        
        [self performAfter:2 block:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.5 animations:^{
                    label.layer.opacity = 0;
                } completion:^(BOOL finished) {
                    [label removeFromSuperview];
                }];
            });
        }];
    }
}

- (void)br_addRightEditingItem
{
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(br_editHandle:)];
    self.navigationItem.rightBarButtonItem = buttonItem;
}

- (void)br_editHandle:(UIBarButtonItem *) item
{
    if (item.tag == 0) {
        item.tag = 1;
        item.title = @"完成";
    }else
    {
        item.tag = 0;
        item.title = @"编辑";
    }
}

- (void)br_addNewButton:(BOOL) isShow btnTitle:(NSString *) title
{
    const int buttonTag = 10002;
    UIButton *oldButton = [self.view viewWithTag:buttonTag];
    if (!oldButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:title forState:UIControlStateNormal];
        button.tag = buttonTag;
        button.backgroundColor = [UIColor lightGrayColor];
        button.height = 40;
        button.width = self.view.width;
        button.bottom = self.view.bottom-64;
        [self.view addSubview:button];
        [button addTarget:self action:@selector(br_newButtonHandle:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (isShow) {
        oldButton.hidden = NO;
    }else
    {
        oldButton.hidden = YES;
    }
}

- (void)br_newButtonHandle:(UIButton *)button
{
    
}

@end
